package com.bootdo.lvyou.controller;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.Document;

import com.bootdo.common.utils.DocumentUtil;
import com.bootdo.lvyou.domain.LvyouCateClassify;
import com.bootdo.lvyou.domain.LvyouCity;
import com.bootdo.lvyou.service.LvyouCateClassifyService;
import com.bootdo.lvyou.service.LvyouCateImgService;
import com.bootdo.lvyou.service.LvyouCityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou.domain.LvyouCate;
import com.bootdo.lvyou.service.LvyouCateService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import sun.applet.Main;

/**
 * 美食表
 * 
 * @author lujingqian
 * @email 965858129@qq.com
 * @date 2019-10-10 13:45:13
 */
 
@Controller
@RequestMapping("/lvyou/lvyouCate")
public class LvyouCateController extends BaseController{


	@Autowired
	private LvyouCateService lvyouCateService;
	@Autowired
	private LvyouCityService lvyouCityService;
	@Autowired
	private LvyouCateClassifyService lvyouCateClassifyService;
	@Autowired
	private LvyouCateImgService lvyouCateImgService;

	@ModelAttribute
	public LvyouCate get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouCateService.get(id);
		}else{
			return new LvyouCate();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou/lvyouCate','lvyou:lvyouCate:lvyouCate',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou:lvyouCate:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou:lvyouCate:lvyouCate")	
	String LvyouCate(LvyouCate lvyouCate,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouCate/lvyouCate";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:lvyouCate:lvyouCate")
	public PageUtils list(@RequestParam Map<String, Object> params){
		if(null!=params.get("cityId")){
			String cityId = (String) params.get("cityId");
			if(cityId.equals("1")||cityId.equals("-1")){
				params.put("cityId",null);
			}
		}
		//查询列表数据
        Query query = new Query(params);
		List<LvyouCate> lvyouCateList = lvyouCateService.list(query);
		int total = lvyouCateService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouCateList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou:lvyouCate:edit")
	String add(LvyouCate lvyouCate,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouCate/lvyouCateedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouCate lvyouCate) {
		return R.ok().put("i",lvyouCate);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou:lvyouCate:edit")
	public R save( LvyouCate lvyouCate){

		lvyouCate.setProfile(DocumentUtil.setWidth(lvyouCate.getProfile()));

		Long cityId = lvyouCate.getCityId();				//获取到城市id
		LvyouCity lvyouCity = lvyouCityService.get(new Long(cityId).intValue());//根据城市id查询对应的对象
		if(lvyouCity.getParentId()==1){//判断是否为 市级   市级parentId 为1
			lvyouCate.setCityName(lvyouCity.getName());
			lvyouCate.setProvincialName(lvyouCity.getName());
			lvyouCate.setProvincialId(cityId);
		}else{							//否则 将递归查询出 市级 填入市级   cityid 填入 cityId
			lvyouCate.setCityName(lvyouCity.getName());
			int recursion = recursion(cityId);
			LvyouCity lvyouCity1 = lvyouCityService.get(recursion);
			lvyouCate.setProvincialName(lvyouCity1.getName());
			lvyouCate.setProvincialId(lvyouCity1.getId().longValue());
		}



		//完善美食分类信息
		LvyouCateClassify lvyouCateClassify = lvyouCateClassifyService.get(lvyouCate.getClassifyId());
		lvyouCate.setClassifyName(lvyouCateClassify.getName());
		String mobile = lvyouCate.getMobile();
		if(mobile.length()>15){
			return R.error("请填写正确的联系方式");
		}
		int number = isNumber(lvyouCate);

		lvyouCate.setGive(number);
		if(lvyouCateService.save(lvyouCate)>0){
			return R.ok();
		}
		return R.error();
	}



	public int isNumber(LvyouCate lvyouCate){
		int number=0;
		try {
			int b = sAllFieldNull(lvyouCate);
			if(b>20){
				number+=1;
			}
		}catch (Exception e){

		}
		String keyword = lvyouCate.getKeyword();
		String profile = lvyouCate.getProfile();
		if(keyword!=null){
			if(keyword.length()>20){
				number+=1;
			}
		}
		if(profile!=null){
			if(profile.length()>50){
				number+=1;
			}
		}
		Integer isHot = lvyouCate.getIsHot();
		if(isHot!=null){
			if(isHot==1){
				number+=1;
			}

		}
		return number;
	}

	public static  int sAllFieldNull(Object obj) throws Exception{
		Class stuCla = (Class) obj.getClass();// 得到类对象
 		Field[] fs = stuCla.getDeclaredFields();//得到属性集合
 		int flag = 0;
 			for (Field f : fs) {//遍历属性
				f.setAccessible(true); // 设置属性是可以访问的(私有的也可以)
 				Object val = f.get(obj);// 得到此属性的值
 					if(val!=null) {//只要有1个属性不为空,那么就不是所有的属性值都为空
						flag+=1;
  					}
 			}
 				return flag;
 			}

	/**
	 * 递归查询市级id
	 * @param id
	 * @return
	 */
	public int recursion(Long id){
		LvyouCity lvyouCity = lvyouCityService.get(new Long(id).intValue());
		if(lvyouCity!=null){
			if(lvyouCity.getParentId()!=1){
				int recursion = recursion(lvyouCity.getParentId().longValue());
				return recursion;
			}else{
				return lvyouCity.getId();
			}
		}else{
			return -1;
		}

	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouCate:edit")
	public R remove( Long id){
		if(lvyouCateService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouCate:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouCateService.batchRemove(ids);
		return R.ok();
	}

}